

#include "init.h"


int connectPOP()
{
    int sock;
    struct sockaddr_in dest;
    struct hostent *hp, *gethostbyname();

    
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0)
    {
        perror("Cannot open the socket");
        exit(1);
    }

    
    dest.sin_family = AF_INET;
    dest.sin_port = htons(110);

    hp = gethostbyname("pop.126.com");
    if (hp == 0)
    {
        perror("hostname error");
        exit(1);
    }

    bcopy(hp->h_addr, &dest.sin_addr, hp->h_length);

    
    if (connect(sock, (struct sockaddr *)&dest, sizeof(dest)) < 0)
    {
        perror("error connection\n");
        exit(1);
    }
    else
    {
        printf("Successfully connected!\n\n");
    }
    
    char rec[1024];
    int n = read(sock, rec, sizeof(rec));
    printf("%s", rec);
    bzero(rec, strlen(rec));

    return sock;
}
int login(char userid[50], char pswd[20], int sock)
{

    char user[50];
    char pass[30], msg[7] = "RETR ";
    char rec[1024], list[6] = "LIST\r\n";
    bzero(user, sizeof(user));
    bzero(pass, sizeof(pass));

    
    strcpy(user, "user ");
    strcat(user, userid);
    strcat(user, "\r\n");

    
    strcpy(pass, "pass ");
    strcat(pass, pswd);
    strcat(pass, "\r\n");

    
    int m = write(sock, user, strlen(user));
    int n = recv(sock, rec, sizeof(rec),0);
    memset(rec, 0, strlen(rec));

    
    m = write(sock, pass, strlen(pass));
    n = recv(sock, rec, sizeof(rec),0);

    if (rec[0] == '-')
    {
        printf("\nWrong username or password, please try again\n\n");
        return 0;
    }
    bzero(rec, sizeof(rec));

    
    return 1;
}